export const Cart = {
  state: {
    cartList: [],
    collectionList: [],
    stateAll: false,
  },
  mutations: {
    ADD_CART(state, product) {
      state.cartList.push(product);
    },
    //添加收藏
    ADD_COLLECTION(state, product) {
      state.collectionList.push(product);
    },
    // 设置购物车全选功能
    SET_STATE_ALL(state) {
      state.stateAll = !state.stateAll;
      state.cartList.forEach((item) => (item.state = state.stateAll));
    },

    // 设置购物车单选功能
    SET_STATE_SINGLE(state) {
      state.stateAll = state.cartList.every((item) => item.state);
    },

    DELETE_PRODUCT(state, id) {
      let index = state.cartList.findIndex((item) => item.id == id);
      state.cartList.splice(index, 1);
    },
    // 取消收藏
    DELETE_COLLECTION(state, id) {
      let index = state.collectionList.findIndex((item) => item.id == id);
      state.collectionList.splice(index, 1);
    },
  },
  actions: {
    addCart({ commit }, product) {
      commit("ADD_CART", product);
    },
    //添加收藏
    addCollection({ commit }, product) {
      commit("ADD_COLLECTION", product);
    },
    //取消收藏
    deleteCollection({ commit }, id) {
      commit("DELETE_COLLECTION", id);
    },
    setStateAll({ commit }) {
      commit("SET_STATE_ALL");
    },
    setSingleState({ commit }) {
      commit("SET_STATE_SINGLE");
    },
    deleteProduct({ commit }, id) {
      commit("DELETE_PRODUCT", id);
    },
  },
};
